﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Book
{
    public partial class FrmBookDetail : DevComponents.DotNetBar.Office2007Form
    {
        public ActionName Action { get; set; }
        public int _bookID { get; set; }

        BookBUS bookBUS;

        public FrmBookDetail()
        {
            InitializeComponent();
            btnUpdate.Location = btnAdd.Location;
            dbiBookCost.DisplayFormat = Global.NumberFormat;
            dbiBookCost.Value = 0;
            dbiReceivePrice.DisplayFormat = Global.NumberFormat;
            dbiReceivePrice.Value = 0;
            cmbAuthor.DisplayMember = Author.AuthorName.ToString();
            cmbAuthor.ValueMember = Author.AuthorID.ToString();
            cmbCategory.DisplayMember = Category.CategoryName.ToString();
            cmbCategory.ValueMember = Category.CategoryID.ToString();
            bookBUS = new BookBUS();
        }

        private void FrmBookDetail_Load(object sender, EventArgs e)
        {
            LoadCategoryList();
            LoadAuthorList();

            if (Action == ActionName.Insert)
            {
                DocSequenceBUS docSeqBUS = new DocSequenceBUS();
                txtBookCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Book.ToString());
            }
            else
            {
                BookDTO bookDto = bookBUS.GetBookByID(_bookID);
                txtBookCode.Text = bookDto.BookCode;
                txtBookName.Text = bookDto.BookName;
                cmbAuthor.SelectedValue = bookDto.AuthorID;
                cmbCategory.SelectedValue = bookDto.CategoryID;
                dbiBookCost.Value = bookDto.BookCost;
                dbiReceivePrice.Value = bookDto.ReceivePrice;
            }
            ProcessButton();
        }

        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtBookName.Text))
            {
                Global.SetMessage(lblMessage, "Tên sách không được trống!", false);
                txtBookName.Focus();
                return false;
            }
            else if (dbiBookCost.Value <= 0)
            {
                Global.SetMessage(lblMessage, "Giá bán phải lớn hơn 0.", false);
                dbiBookCost.Focus();
                return false;
            }
            else if (dbiReceivePrice.Value <= 0)
            {
                Global.SetMessage(lblMessage, "Giá nhập phải lớn hơn 0.", false);
                dbiReceivePrice.Focus();
                return false;
            }

            return true;
        }

        private void LoadCategoryList()
        {
            CategoryBUS cateBUS = new CategoryBUS();
            cmbCategory.DataSource = cateBUS.GetCategoryList();
        }

        private void LoadAuthorList()
        {
            AuthorBUS authorBUS = new AuthorBUS();
            cmbAuthor.DataSource = authorBUS.GetNewBindingListAuthor();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            Book.FrmCategoryDetail frm = new FrmCategoryDetail { Action = ActionName.Insert };
            frm.ShowDialog();

            LoadCategoryList();
        }

        private void btnAddAuthor_Click(object sender, EventArgs e)
        {
            Book.FrmAuthorDetail frm = new FrmAuthorDetail { Action = ActionName.Insert };
            frm.ShowDialog();

            LoadAuthorList();
        }

        private BookDTO MapBookDTOFromUI()
        {
            BookDTO bookDto = new BookDTO
            {
                BookCode = txtBookCode.Text,
                BookName = txtBookName.Text,
                BookCost = float.Parse(dbiBookCost.Value.ToString()),
                ReceivePrice = float.Parse(dbiReceivePrice.Value.ToString()),
                AuthorID = int.Parse(cmbAuthor.SelectedValue.ToString()),
                CategoryID = int.Parse(cmbCategory.SelectedValue.ToString())
            };

            return bookDto;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(!CheckDataInput())
            {
                return;
            }

            BookDTO bookDto = MapBookDTOFromUI();

            _bookID = bookBUS.InsertBook(bookDto);

            if (_bookID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm sách mới thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Book.FrmBookList frm = (Book.FrmBookList)Application.OpenForms["FrmBookList"];
                if (frm != null)
                {
                    frm.BookListChangeAfterInsert(_bookID);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm sách thất bại!", false);
            }
        }

        private void ResetForm()
        { 
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            txtBookCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Book.ToString());
            Action = ActionName.Insert;
            ProcessButton();

            txtBookName.Clear();
            LoadAuthorList();
            LoadCategoryList();
            txtBookName.Focus();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            BookDTO bookDto = MapBookDTOFromUI();
            bookDto.BookID = _bookID;
            if (bookBUS.UpdateBook(bookDto))
            {
                Global.SetMessage(lblMessage, "Sửa thông tin sách thành công!", true);
                Book.FrmBookList frm = (Book.FrmBookList)Application.OpenForms["FrmBookList"];
                if (frm != null)
                {
                    frm.BookChangeAfterUpdate(_bookID);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Sửa thông tin sách không thành công!", false);
            }
        }
    }
}
